<template>
<!-- 弹框组件 -->
  <div>
    <el-dialog
      :title="{ add: '添加子部门', edit: '编辑部门' }[mode]"
      width="45%"
      :visible.sync="isShow"
      @close="closeEvent"
    >
      <!-- 表单区域 -->
      <el-form ref="form" label-width="100px" :model="from" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="from.name" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="from.code" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="from.manager" @focus="focusEvent">
            <el-option
              v-for="value in managerList"
              :key="value.id"
              :label="value.username"
              :value="value.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="from.introduce" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="noLook">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  sysuserSimpleAPI,
  companyDepartmentAPIPOST,
  companyDepartmentPUTAPI
} from '@/api'
export default {
  props: {
    // 传过来的原始数据
    in_fo: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isShow: false,
      from: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      managerList: [],
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 50, trigger: 'change', message: '输入1-50个字符' },
          {
            // value 当前输入框内的数据
            validator: (rule, value, callback) => {
              if (this.mode == 'add') {
                // 找出点击项子节点数据
                const resultArr = this.in_fo.filter((v) => {
                  if (v.pid === this.item.id) {
                    return true
                  }
                })
                const bol = resultArr.some((v) => {
                  // 判断是否有重名 返回布尔值
                  return v.name === value
                })
                bol
                  ? callback(new Error('请不要输入重复的部门名' + value))
                  : callback()
              } else if (this.mode == 'edit') {
                // 编辑场景

                const resultArr = this.in_fo.filter((v) => {
                  // 找出同级得排除自己 pid相同
                  if (v.pid == this.item.pid && v.id !== this.item.id) {
                    return true
                  }
                })
                const bol = resultArr.some((v) => {
                  return v.name == value
                })
                bol ? callback(new Error('不可修改成重名的部门名')) : callback()
                // 并且不能修改成同级的名字
              }
            }
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 50, trigger: 'change', message: '输入1-50个字符' },
          {
            validator: (rule, value, callback) => {
              if (this.mode == 'add') {
                const code = this.in_fo.some((v) => {
                  return value == v.code
                })
                code ? callback(new Error('已存在')) : callback()
              } else if (this.mode == 'edit') {
                const Code = this.in_fo.some((v) => {
                  return v.code == value && v.id !== this.item.id
                })
                Code ? callback(new Error('请不要输入重复的编码')) : callback()
              }
            }
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'change' }],
        item: '',
        introduce: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 50, trigger: 'change', message: '输入1-50个字符' }
        ]
      },
      mode: 'add'
    }
  },
  mounted() {
    // 监听兄弟组件传过来的值是显示还是隐藏
    this.$bus.$on('addEvent', (show, item, mode) => {
      this.isShow = show
      this.item = item
      this.mode = mode

      if (this.mode == 'edit') {
        this.from = JSON.parse(JSON.stringify(item))
      }
    })
  },
  beforeDestroy() {
    this.$bus.$off('addEvent')
  },
  methods: {
    noLook() {
      this.isShow = false
    },
    // 表单内置事件获取焦点触发  判断有数据就不触发
    async focusEvent() {
      if (this.managerList.length == 0) {
        const res = await sysuserSimpleAPI()
        this.managerList = res.data
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode == 'add') {
            this.from.pid = this.item.id
            await companyDepartmentAPIPOST(this.from)
            this.isShow = false
            this.$emit('getData')
            this.$message.success('验证成功')
          } else if (this.mode == 'edit') {
            await companyDepartmentPUTAPI(this.from)
            this.isShow = false
            this.$emit('getData')
          }
        }
      })
    },
    // 弹窗关闭时触发的回调函数
    closeEvent() {
      this.$refs.form.resetFields()
      this.from = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
    }
  }
}
</script>

<style></style>
